package homework.JButtonExample;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class JButtonExample extends WindowAdapter implements ActionListener
{ JFrame f;
    JButton b;
    JToggleButton bt;
    JTextField tf;
    int tag = 0;
    public static void main(String args[])
    { JButtonExample be = new JButtonExample();
        be.go();
    }
    public void go()
    { f = new JFrame("JButton Example");
        b = new JButton("Sample");
        bt = new JToggleButton("ToggleSample");
//增加 JToggleButton 示例 2021.11.6
        b.addActionListener(this);
        bt.addActionListener(this);
        f.getContentPane().add(b,"South");
        f.getContentPane().add(bt,"North");
        tf = new JTextField();
        f.getContentPane().add(tf,"Center");
        f.addWindowListener(this);
        f.setSize(300,150);
        f.setVisible(true);
    }
    public void actionPerformed(ActionEvent e)
    { String s1 = "You have pressed the Button!";
        String s2 = "You do another time!";
//交替显示两条信息
        if (tag==0)
        { tf.setText(s1);
            tag = 1;
        } else
        { tf.setText(s2);
            tag = 0;
        }
    }
    public void windowClosing(WindowEvent e)
    { // 结束程序运行
        System.exit(0);
    }
}